package cn.ffcs.uec.outStaff.component;

import java.util.HashMap;
import java.util.Map;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Components;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.IdSpace;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Div;
import org.zkoss.zul.ListModel;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Window;

import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.outStaff.component.bean.UecOutStaffTrainListboxBean;
import cn.ffcs.uec.outStaff.constant.UecOutStaffConstants;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.staff.manager.UecTrainManager;
import cn.ffcs.uec.staff.model.UecTrain;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.util.ApplicationContextUtil;
import cn.ffcs.uom.common.util.IPortletInfoProvider;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;
import lombok.Getter;
import lombok.Setter;
/**
 * @Title:人员教育培训 CRUD插件
 * @Description:
 * @Company: FFCS
 * @Version 0.0.1 date:2018年05月27日
 * @since JDK1.6
 */
@Controller
@Scope("prototype")
public class UecOutStaffTrainListboxExt extends Div implements IdSpace {

	private static final long serialVersionUID = -5811398149406451671L;
	/**
	 * 人员基本信息功能 查询出的实体对象
	 */
	private UecOutStaffInfo uecStaffInfo;
	/**
	 * 对应页面上的bean
	 */
	private UecOutStaffTrainListboxBean bean = new UecOutStaffTrainListboxBean();
	/**
	 * 人员教育培训实体
	 */
	private UecTrain uecTrain;
	/**
	 * 人员教育培训查询实体
	 */
	// private UecAss qryUecAss;

	private UecTrainManager uecTrainManager = (UecTrainManager) ApplicationContextUtil
			.getBean("uecTrainManager");

	@Getter
	@Setter
	private IPortletInfoProvider portletInfoProvider;

	public UecOutStaffTrainListboxExt() {
		Executions.createComponents(UecOutStaffConstants.UEC_OUT_STAFF_TRAIN_EXT_LISTBOX_ZUL,
				this, null);
		Components.wireVariables(this, bean);
		Components.addForwards(this, this, '$');

		this.addForward(UecOutStaffConstants.ON_UEC_OUT_STAFF_TRAIN_QUERY, this,
				UecOutStaffConstants.ON_UEC_OUT_STAFF_TRAIN_QUERY_RESPONSE);

		this.addForward(UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_TRAIN, this,
				UecOutStaffConstants.ON_CLEAN_UEC_OUT_STAFF_TRAIN_RESPONSE);
	}

	/**
	 * 初始化
	 */
	public void onCreate() {
		this.setButtonValid(false, false, false, false);
	}

	/**
	 * 选择查询记录
	 * 
	 * @throws Exception
	 */
	public void onSelectUecTrainList() throws Exception {
		if (this.bean.getUecTrainListbox().getSelectedCount() > 0) {
			uecTrain = (UecTrain) bean.getUecTrainListbox().getSelectedItem()
					.getValue();
		}
	}

	/**
	 * 人员基本信息功能，抛出事件， 事件监听
	 * 
	 * @param event
	 */
	public void onUecOutStaffTrainQueryResponse(ForwardEvent event) {
		uecStaffInfo = (UecOutStaffInfo) event.getOrigin().getData();
		if (null != uecStaffInfo) {
            //设置操作按钮，非盟市主管不可操作按钮
            if(PermissionUtil.isNoMainOutStaff()) {
                this.setButtonValid(false, false, false, false);
            }else {
                this.setButtonValid(true, true,true,false);
            }
			uecTrain = new UecTrain();
			uecTrain.setStaffId(uecStaffInfo.getOutStaffId());
			uecTrain.setNmStaffType(BaseUnitConstants.NM_OUT_STAFF_TYPE);
			this.onQueryUecTrain();
		}
	}

	public void onCleanUecOutStaffTrainResponse(ForwardEvent event) {
		clearVariable();
		uecStaffInfo = null;
		ListboxUtils.clearListbox(this.bean.getUecTrainListbox());
		this.bean.getUecTrainListboxPaging().setTotalSize(1);
	}

	/**
	 * 设置按钮
	 * 
	 * @param canAdd
	 * @param candel
	 */
	private void setButtonValid(boolean canView, boolean canAdd,
			boolean canEdit, boolean candel) {
		this.bean.getViewButton().setDisabled(!canView);
		this.bean.getAddButton().setDisabled(!canAdd);
		this.bean.getEditButton().setDisabled(!canEdit);
		this.bean.getDelButton().setDisabled(!candel);
	}

	/**
	 * 查询按钮
	 */
	public void onQueryUecTrain() {
		this.bean.getUecTrainListboxPaging().setActivePage(0);
		this.onQueryUecTrainList();
	}

	/**
	 * 重置按钮
	 */
	public void onResetUecTrain() {
		ListboxUtils.clearListbox(this.bean.getUecTrainListbox());
		this.bean.getUecTrainListboxPaging().setTotalSize(1);
	}
	/**
	 * 查看按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onViewUecTrain() throws Exception, InterruptedException {
		if (!StrUtil.isNullOrEmpty(uecTrain.getId())
				&& !StrUtil.isNullOrEmpty(uecTrain.getId())) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "view");
			map.put("uecTrain", uecTrain);
			Window window = (Window) Executions.createComponents(
					UecOutStaffConstants.UEC_OUT_STAFF_TRAIN_EDIT_ZUL, this, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						//
					}
				}
			});
		} else {
			ZkUtil.showError("请选中要查看的记录", "提示信息");
		}
	}

	/**
	 * 新增按钮
	 * 
	 * @throws Exception
	 * @throws
	 */
	public void onAddUecTrain() throws Exception {
		if (null != uecStaffInfo) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "add");
			map.put("uecOutStaffInfo", uecStaffInfo);
			Window window = (Window) Executions.createComponents(UecOutStaffConstants.UEC_OUT_STAFF_TRAIN_EDIT_ZUL,
					this, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						// 新增或修改完成后，列表从数据库更新
						uecTrain = (UecTrain) event.getData();
						onQueryUecTrainList();
					}
				}
			});
		}
	}

	/**
	 * 编辑按钮
	 * 
	 * @throws Exception
	 * @throws InterruptedException
	 */
	public void onEditUecTrain() throws Exception, InterruptedException {
		if (null != uecTrain && null != uecTrain.getId()) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("opType", "edit");
			map.put("uecTrain", uecTrain);
			Window window = (Window) Executions.createComponents(UecOutStaffConstants.UEC_OUT_STAFF_TRAIN_EDIT_ZUL,
					this, map);
			window.doModal();
			window.addEventListener("onOK", new EventListener() {
				@Override
				public void onEvent(Event event) throws Exception {
					if (event.getData() != null) {
						// 新增或修改完成后，列表从数据库更新
						uecTrain = (UecTrain) event.getData();
						onQueryUecTrainList();
					}
				}
			});
		} else {
			ZkUtil.showError("请选择要编辑的记录", "提示信息");
			return;
		}
	}

	/**
	 * 删除按钮
	 */
	public void onDelUecTrain() {
		if (this.uecTrain != null && uecTrain.getId() != null) {
			ZkUtil.showQuestion("确定要删除吗?", "提示信息", new EventListener() {
				public void onEvent(Event event) throws Exception {
					Integer result = (Integer) event.getData();
					if (result == Messagebox.OK) {
						uecTrain.removeOnly();
						PubUtil.reDisplayListbox(bean.getUecTrainListbox(), uecTrain, "del");
						uecTrain = null;
					}
				}
			});
		} else {
			ZkUtil.showError("请选择要删除的记录", "提示信息");
			return;
		}
	}

	/**
	 * 翻页
	 */
	public void onUecTrainListboxPaging() {
		this.onQueryUecTrainList();
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		uecTrain = null;
	}

	/**
	 * 查询
	 */
	public void onQueryUecTrainList() {
		if (null != uecTrain) {
			PageInfo pageInfo = uecTrainManager.queryPageInfoByUecTrain(uecTrain,
					this.bean.getUecTrainListboxPaging().getActivePage() + 1,
					this.bean.getUecTrainListboxPaging().getPageSize());
			ListModel list = new BindingListModelList(pageInfo.getDataList(),
					true);
			this.bean.getUecTrainListbox().setModel(list);
			this.bean.getUecTrainListboxPaging().setTotalSize(
					pageInfo.getTotalCount());
		}
	}

}
